// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    ifclist: [{
        'title': '公交路线',
        'img': 'https://img1.baidu.com/it/u=1242055722,429600292&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
        'url': '../busline/busline'
      },
      {
        'title': '站点查询',
        'img': 'https://img2.baidu.com/it/u=1663304304,898340817&fm=253&fmt=auto&app=138&f=PNG?w=256&h=256',
        'url': '../zhandian/index'
      },
      {
        'title': '换乘查询',
        'img': 'https://img0.baidu.com/it/u=3539984565,984282060&fm=253&fmt=auto&app=138&f=JPEG?w=256&h=256',
        'url': '../huanchen/index'
      },
      {
        'title': '路线规划',
        'img': 'https://img2.baidu.com/it/u=1271893833,125240720&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
        'url': '../luxian/index'
      },
      {
        'title': '地点选择',
        'img': 'https://img2.baidu.com/it/u=1344348941,9235289&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
        'url': '1'
      },
      {
        'title': '停车场',
        'img': 'https://img2.baidu.com/it/u=2040619492,2932259782&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
        'url': '../tingche/index'
      },
      {
        'title': '附近美食',
        'img': 'https://img1.baidu.com/it/u=3142930168,858814254&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=441',
        'url': '../fujin/index?ifc=美食'
      },
      {
        'title': '附近住宿',
        'img': 'https://img2.baidu.com/it/u=1936191263,712660058&fm=253&fmt=auto&app=138&f=JPEG?w=256&h=256',
        'url': '../fujin/index?ifc=住宿'
      }

    ],
    currentIndex: 0,
    imglist: ['https://img2.baidu.com/it/u=2876434450,423714296&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=434', 'https://img0.baidu.com/it/u=1986836363,2816743741&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=310', 'https://img0.baidu.com/it/u=2757914890,2950021813&fm=253&fmt=auto&app=138&f=JPEG?w=489&h=293']
  },

  tourl: function (e) {
    var url = e.currentTarget.dataset.url;
    if (url == 1) { //只有一个是使用插件的
      this.getCenterLocation()
    } else {
      wx.navigateTo({
        url: url,
      })
    }

  },

  getCenterLocation() {
    var that = this;
    wx.authorize({
      scope: 'scope.userLocation',
      complete: function (res) {
        console.log(res)
        wx.chooseLocation({
          success(str) {
            console.log(str)
            var key = '64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD';
            //发送请求通过经纬度反查地址信息
            var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + str.latitude + "," + str.longitude + "&key=" + key + "&get_poi=1";
            wx.request({
              url: getAddressUrl,
              success: function (result) {
                var province = result.data.result.address_component.province;
                var city = result.data.result.address_component.city;
                var district = result.data.result.address_component.district;
                var address = result.data.result.formatted_addresses.recommend;
                console.log('省市县:' + province + city + district)
                console.log('地址：' + JSON.stringify(result.data.result))
                let endPoint = JSON.stringify({ //终点
                  name: result.data.result.address,
                  latitude: result.data.result.location.lat,
                  longitude: result.data.result.location.lng,
                })
                var key = "64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD";
                let referer = '地摊';
                wx.navigateTo({
                  url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint= ' + endPoint
                });
                that.setData({
                  "input[2].val": [province, city, district],
                  "input[3].val": address
                })
              }
            })
          }
        })
      }
    })

  },

  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    var that = this;


  },


})